package cn.myfirefly.core.tag.auth;

import java.util.Map;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.TagSupport;

import cn.myfirefly.core.model.Identity;
import cn.myfirefly.core.model.Permission;
import cn.myfirefly.utils.StrUtil;

public class HasTag extends TagSupport {

	/**
	 * 
	 */
	private static final long serialVersionUID = 5282428544387260981L;

	// 功能编号，例如：customer
	private String funcId;
	
	// 动作，支持多个动作，用逗号分隔，例如：/customer/list.do,/customer/mylist.do
	private String action;
	
	@SuppressWarnings("unchecked")
	public int doStartTag() throws JspException {
		Identity identity = (Identity)this.pageContext.getSession().getAttribute("identity");
		if (identity.isAdmin()) {
			return EVAL_BODY_INCLUDE;
		}
		Map<String, Permission> map = (Map<String, Permission>)this.pageContext.getSession().getAttribute("permissions");
		Permission permission = map.get(funcId);
		if (permission == null) {
			return SKIP_BODY;
		}
		String[] anctions = action.split(",");
		for (int i = 0; i < anctions.length; i++) {
			if (StrUtil.notBlank(anctions[i])) {
				if (permission.isPermit(anctions[i].trim())) {
					return EVAL_BODY_INCLUDE;
				}
			}
		}
		return SKIP_BODY;
	}

	public String getFuncId() {
		return funcId;
	}

	public void setFuncId(String funcId) {
		this.funcId = funcId;
	}

	public String getAction() {
		return action;
	}

	public void setAction(String action) {
		this.action = action;
	}
}
